#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>

#define BUF_SIZE 4
#define EPOLL_SIZE 50
void error_handling( char * buf);

int main( int argc, char *argv[])
{
    int serv_sock;
    int clnt_sock;
    struct sockaddr_in serv_adr;
    struct sockaddr_in clnt_adr;

    socklen_t adr_sz;
    int str_len;
    int i;
    char buf[BUF_SIZE];

    struct epoll_event *epoll_events;
    struct epoll_event event;
    int epfd;
    int event_cnt;

    if ( 2 != argc ) {
        printf("Usage: %s <port>\n", argv[0]);
        exit( 1 );
    }
    serv_sock = socket( PF_INET, SOCK_STREAM, 0);
    memset( &serv_adr, 0, sizeof(serv_adr));
    serv_adr.sin_family = AF_INET;
    serv_adr.sin_addr.s_addr = htonl( INADDR_ANY );
    serv_adr.sin_port = htons( atoi( argv[1]));

    if (-1 == bind(serv_sock, (struct sockaddr*) &serv_adr, sizeof(serv_adr)))
        error_handling("bind() error");
    if (-1 == listen(serv_sock, 5))
        error_handling("listen() error");
    
    epfd = epoll_create( EPOLL_SIZE);
    epoll_events = malloc( sizeof(struct epoll_event) * EPOLL_SIZE);

    event.events = EPOLLIN;
    event.data.fd = serv_sock;
    epoll_ctl( epfd, EPOLL_CTL_ADD, serv_sock, &event);

    while (1)
    {
        event_cnt = epoll_wait( epfd, epoll_events, EPOLL_SIZE, -1);
        if ( event_cnt == -1) {
            puts("epoll_wait() error");
            break;
        }
        puts("return epoll_wait");
        for ( i = 0; i < event_cnt; i++) {
            if ( epoll_events[i].data.fd == serv_sock) {
                adr_sz = sizeof( clnt_adr );
                clnt_sock = accept( serv_sock, (struct sockaddr *)&clnt_adr, &adr_sz);
                event.events = EPOLLIN;
                event.data.fd = clnt_sock;
                epoll_ctl(epfd, EPOLL_CTL_ADD, clnt_sock, &event);
                printf("connected client: %d \n", clnt_sock);
            }
            else {
                str_len = read(epoll_events[i].data.fd, buf, BUF_SIZE);
                if ( str_len == 0) {
                    epoll_ctl(epfd, EPOLL_CTL_DEL, epoll_events[i].data.fd, NULL);
                    close(epoll_events[i].data.fd);
                    printf("closed client: %d \n", epoll_events[i].data.fd);
                }
                else {
                    write( epoll_events[i].data.fd, buf, str_len);
                }
            }
        }

    }


    close(serv_sock);
    close(epfd);

    return 0;
}

void error_handling( char *buf )
{
    fputs( buf, stderr );
    fputc( '\n', stderr );
    exit ( 1 );
}